package com.springboot.utils;

import com.auth0.jwt.algorithms.Algorithm;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Calendar;
import java.util.Map;

public class JJwtUtils {

    private static final String SIGN = "@WWQss232";

    /**
     * 生成token
     */
    public static String getToken(Map<String,String> map){
        Calendar instance = Calendar.getInstance();
//        instance.add(Calendar.DATE, 7);
        instance.add(Calendar.SECOND, 60);

        JwtBuilder builder = Jwts.builder();
        //自定义参数
        for(String key:map.keySet()){
            builder.claim(key, map.get(key));
        }
        builder.setExpiration(instance.getTime())
                .signWith(SignatureAlgorithm.HS256,SIGN);
        String token = builder.compact();
        System.out.println("toekn"+ token);
        return token;
    }
    /**
     * 解析token
     */
    public static Claims verifyToken(String token){
        Claims claims = (Claims)Jwts.parser()
                .setSigningKey(SIGN)
                .parse(token)
                .getBody();
        System.out.println("userId"+ claims.get("userId").toString());
        System.out.println("username"+ claims.get("username").toString());
        return claims;
    }
}
